﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using System.Text.RegularExpressions;
using Helper.Bussiness;
using com.SEO.Model;
using com.SEO.Business;

namespace SEO_Server.Pages.User
{
    public partial class EditSitePage : System.Web.UI.Page
    {
        /// <summary>
        /// User IDs
        /// </summary>
        public List<string> UserIDs
        {
            get
            {
                if (ViewState["UserIDs"] == null)
                {
                    return new List<string>();
                }

                return (List<string>)ViewState["UserIDs"];
            }
            set
            {
                ViewState["UserIDs"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string ids = Request.QueryString["UserIDs"];
                string[] idsArray = ids.Split(new char[] { '|' });
                List<string> lstID = new List<string>();
                foreach (string id in idsArray)
                {
                    if (id.Length > 0)
                    {
                        lstID.Add(id);
                    }
                }

                UserIDs = lstID;

                BindArea();
            }
        }

        #region Private Methods

        /// <summary>
        /// Bind the text area control by |UserIDs|.
        /// </summary>
        private void BindArea()
        {
            if (UserIDs.Count == 1)
            {
                SiteManage siteManage = new SiteManage();
                UserManage userManage = new UserManage();
                SiteInfo siteInfo = null;
                UserInfo userInfo = userManage.GetUserInfoByID(UserIDs[0]);
                if (userInfo != null)
                {
                    if (!userInfo.SiteId.Equals(Guid.Empty))
                    {
                        siteInfo = siteManage.GetSiteInfoByID(userInfo.SiteId.ToString());
                        if (siteInfo != null)
                        {
                            txtAreaURL.Text = siteInfo.SiteURL;
                        }
                    }
                }
            }
        }

        #endregion

        #region Page Events

        protected void btnEnter_Click(object sender, EventArgs e)
        {
            UserManage userManage = new UserManage();
            SiteManage siteManage = new SiteManage();

            UserInfo userInfo = null;
            SiteInfo siteInfo = null;

            // Find the site from DB, if the site is not exists,
            // then create an new one.
            List<SiteInfo> lstSites = siteManage.GetSiteByCondition(" SiteURL = '" + txtAreaURL.Text.Trim() + "'");
            if (lstSites.Count > 0)
            {
                siteInfo = lstSites[0];
            }
            else
            {
                siteInfo = new SiteInfo();
                siteInfo.SiteId = Guid.NewGuid();
                siteInfo.SiteURL = txtAreaURL.Text.Trim();
            }

            // Set the value of |SiteID| property of the |userInfo| install.
            foreach (string id in UserIDs)
            {
                userInfo = userManage.GetUserInfoByID(id);
                if (userInfo != null)
                {
                    Guid siteID = userInfo.SiteId;
                    userInfo.SiteId = siteInfo.SiteId;
                    userManage.UpdateUserInfo(userInfo);

                    SiteManage.CheckSite(siteID);
                }
            }
            
            // Add the siteInfo into DB.
            int result = -1;
            if (lstSites.Count == 0) {
                result = siteManage.AddSiteInfo(siteInfo);
            }

            if (result == -1)
            {


                this.btnClose_Click(null, null);
            }
            else
            {
                Alert.Show("修改站點失敗!");
            }
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHidePostBackReference());
        }

        #endregion
    }
}